package com.plum.admin.modules.sys.controller;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.google.common.collect.Maps;
import com.plum.admin.common.annotation.Log;
import com.plum.admin.common.enums.LogType;
import com.plum.admin.common.util.R;
import com.plum.admin.modules.sys.entity.SysDict;
import com.plum.admin.modules.sys.service.ISysDictService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import com.plum.admin.base.BaseController;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 字典表 前端控制器
 * </p>
 *
 * @author 子丶沫
 * @since 2021-08-13
 */
@RestController
@RequestMapping("/sys/dict")
@Api(tags = "字典管理")
public class SysDictController extends BaseController {
    @Autowired
    private ISysDictService dictService;

    @GetMapping
    @ApiOperation(value = "字典列表")
    @PreAuthorize("hasAnyAuthority('sys:dict:list')")
    @Log(type = LogType.OPERATION,title = "字典列表")
    public R<Object> dictList(String dictName){
        return R.ok(
                dictService.list(Wrappers.<SysDict>lambdaQuery()
                        .like(StrUtil.isNotBlank(dictName),SysDict::getDictName,dictName))
                        .stream().map(e->{
                    Map<String,String> param= Maps.newHashMap();
                    param.put("label",e.getDictName());
                    param.put("key",e.getId());
                    param.put("code",e.getDictCode());
                    return param;
                }).collect(Collectors.toList())
        );
    }
    @GetMapping("/checkDictCode/{dictCode}")
    @ApiOperation("校验字典编码唯一性")
    @Log(type = LogType.OPERATION,title = "校验字典编码唯一性")
    public R<Object> checkDictCode(@PathVariable String dictCode){
        List<SysDict> dictList = dictService.list(Wrappers
                .<SysDict>lambdaQuery()
                .eq(StrUtil.isNotBlank(dictCode), SysDict::getDictCode, dictCode));
        return R.ok(dictList.size()>0);
    }
    @ApiOperation("新增字典")
    @PostMapping
    @PreAuthorize("hasAnyAuthority('sys:dict:save')")
    @Log(type = LogType.OPERATION,title = "新增字典")
    public R<Object> saveDict(@RequestBody SysDict dict){
        return R.ok(dictService.save(dict));
    }
    @ApiOperation("删除字典")
    @DeleteMapping
    @PreAuthorize("hasAnyAuthority('sys:dict:delete')")
    @Log(type = LogType.OPERATION,title = "删除字典")
    public R<Object> deleteDict(String ids){
        List<String> idList = Arrays.stream(ids.split(",")).collect(Collectors.toList());
        return R.ok(dictService.deleteDict(idList));
    }
    @ApiOperation("通过字典类型加载字典选项")
    @GetMapping("/findDictList")
    public R<Object> findDictList(){
        return R.ok(dictService.findDictList());
    }
}
